package com.jxl_dyw.UI;

import com.jxl_dyw.entity.Dept;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class EditDeptUI {


    public  EditDeptUI(Dept dept) {
        // 创建 JFrame
        JFrame frame = new JFrame("编辑部门");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 关闭时只关闭当前窗口，不退出整个程序
        frame.setSize(800, 600); // 增大窗口尺寸
        frame.setLocationRelativeTo(null); // 居中显示窗口

        // 使用 GridBagLayout 进行布局
        frame.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10); // 增大组件之间的间距

        // 创建一个面板用于存放所有输入字段
        JPanel inputPanel = new JPanel(new GridBagLayout());
        gbc.gridx = 0;
        gbc.gridy = 0;

        // 设置全局字体，使所有组件的文字更大
        Font font = new Font("宋体", Font.PLAIN, 18);

        // 创建组件并添加到面板
        JLabel deptIdLabel = new JLabel("部门编号:");
        JTextField deptIdField = new JTextField(20);
        deptIdLabel.setFont(font);
        deptIdField.setFont(font);
        addComponent(inputPanel, deptIdLabel, deptIdField, gbc);

        JLabel deptNameLabel = new JLabel("部门名称:");
        JTextField deptNameField = new JTextField(20);
        deptNameLabel.setFont(font);
        deptNameField.setFont(font);
        addComponent(inputPanel, deptNameLabel, deptNameField, gbc);

        JLabel buildPeopleLabel = new JLabel("负责人:");
        JTextField buildPeopleField = new JTextField(20);
        buildPeopleLabel.setFont(font);
        buildPeopleField.setFont(font);
        addComponent(inputPanel, buildPeopleLabel, buildPeopleField, gbc);

        JLabel buildTimeLabel = new JLabel("成立日期:");
        JTextField buildTimeField = new JTextField(20);
        buildTimeLabel.setFont(font);
        buildTimeField.setFont(font);
        addComponent(inputPanel, buildTimeLabel, buildTimeField, gbc);

        // 将获取的对象的数据放入各个输入框内
        deptIdField.setText(dept.getDeptId());
        deptNameField.setEditable(false); // 部门名称不允许修改
        deptNameField.setText(dept.getDeptName());
        buildPeopleField.setText(dept.getBuildPeople());
        buildTimeField.setText(dept.getBuildTime());

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton saveButton = new JButton("修改");
        saveButton.setFont(font); // 设置按钮字体
        buttonPanel.add(saveButton);

        // 添加按钮点击事件监听器
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入的部门信息
                String deptId = deptIdField.getText().trim();
                String deptName = deptNameField.getText().trim();
                String buildPeople = buildPeopleField.getText().trim();
                String buildTime = buildTimeField.getText().trim();
                // 判断部门编号是否为纯数字
                if (!deptId.matches("\\d+")) {
                    JOptionPane.showMessageDialog(frame, "部门编号必须为纯数字！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //对日期输入的格式进行验证
                if (!buildTime.matches("\\d{4}-\\d{2}-\\d{2}")) {
                    JOptionPane.showMessageDialog(frame, "日期格式不正确，请输入yyyy-MM-dd格式的日期！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                // 验证输入是否为空
                if (deptId.isEmpty()||deptName.isEmpty() || buildPeople.isEmpty() || buildTime.isEmpty()) {
                    JOptionPane.showMessageDialog(frame, "请填写所有必填项！", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                // 更新 Dept 对象
                Dept dept = new Dept(deptId, deptName, buildPeople, buildTime);
                //添加到哈希表中
                DeptUI.getDeptMap().put(dept.getDeptName(), dept);

                // 刷新部门表格
                DeptUI.refreshTable();

                // 将部门信息写入文件
                try {
                    DeptUI.writeDeptToFile(DeptUI.filePath);
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(frame, "保存部门信息失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                //清空输入框
                deptIdField.setText("");
                deptNameField.setText("");
                buildPeopleField.setText("");
                buildTimeField.setText("");
                // 关闭页面
                frame.dispose();
            }
        });

        // 将输入面板和按钮面板添加到主窗口
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2; // 占两列
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        frame.add(inputPanel, gbc);

        gbc.gridy++;
        gbc.gridwidth = 1; // 占一列
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.weighty = 0.1;
        frame.add(buttonPanel, gbc);

        // 设置窗口可见
        frame.setVisible(true);
    }


    // 辅助方法：添加标签和输入框到面板
    private void addComponent(JPanel panel, JLabel label, JComponent textField, GridBagConstraints gbc) {
        gbc.gridx = 0;
        gbc.gridy++;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(label, gbc);

        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(textField, gbc);
    }

}